/**
 * Created on [2022-11-11] by zhengrr
 *
 * 项目名称: btfvilg
 *
 * 本程序版权属于福建慧政通信息科技有限公司所有。
 *
 * 任何组织和个人未经福建慧政通信息科技有限公司许可与授权,不得擅自传播、复制、更改该程序的内容。
 * 本程序受版权法和国际条约的保护。如未经授权而擅自复制或传播本程序(或其中任何部分),
 * 将受到严厉的刑事及民事制裁，并将在法律许可的范围内受到最大可能的起诉!
 *
 * ©2021 福建慧政通信息科技有限公司
 */
package com.sunnada.btfvilg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunnada.btfvilg.entity.Address;
import com.sunnada.btfvilg.mapper.AddressMapper;
import com.sunnada.btfvilg.model.request.AddrParamVo;
import com.sunnada.btfvilg.model.response.AddrVo;
import com.sunnada.btfvilg.model.response.AddressListVo;
import com.sunnada.btfvilg.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 *  服务实现类
 *
 * @author zhengrr
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {


    @Autowired(required = false)
    private AddressMapper addressMapper;

    @Override
    public AddrVo addAddress(AddrParamVo addr) {
        Address address = new Address();
        address.setAid(UUID.randomUUID().toString().replaceAll("-",""));
        address.setUid(addr.getUid());
        address.setProvince(addr.getProvince());
        address.setCity(addr.getCity());
        address.setCounty(addr.getCounty());
        address.setTown(addr.getTown());
        address.setLocation(addr.getLocation());
        addressMapper.insert(address);
        AddrVo addrVo = new AddrVo(address.getAid(), address.getUid());
        return addrVo;
    }

    @Override
    public void delAddress(String aid) {
        addressMapper.deleteById(aid);
    }

    @Override
    public void updateAddress(AddrParamVo addr) {
        Address address = new Address();
        address.setAid(addr.getAid());
        address.setProvince(addr.getProvince());
        address.setCity(addr.getCity());
        address.setCounty(addr.getCounty());
        address.setTown(addr.getTown());
        address.setLocation(addr.getLocation());
        addressMapper.updateById(address);
    }

    @Override
    public AddressListVo getAddrByUid(String uid, Integer page, Integer numPerPage) {
        AddressListVo addrVo = new AddressListVo();
        addrVo.setPage(page);
        addrVo.setNumPerPage(numPerPage);
        QueryWrapper<Address> qw = new QueryWrapper<>();
        //Integer count = addressMapper.selectCount(qw);
        List<Address> addrList = addressMapper.getAddrByUid(uid, (page-1)*numPerPage, numPerPage);
        Integer count =addrList.size();
        addrVo.setTotal(count);
        addrVo.setAddressList(addrList);
        return addrVo;
    }

    @Override
    public Address getAddrById(String aid) {
        return addressMapper.selectById(aid);
    }

}
